#!/bin/bash
############################################################################ ###
#@Copyright     Copyright (c) Imagination Technologies Ltd. All Rights Reserved
#@License       MIT
# The contents of this file are subject to the MIT license as set out below.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#### ###########################################################################

# Auto-generated for owl_linux from rogueddk_1.5@3604260
#

load_pvr()
{
	# Load 3rd party module(s).
	#
	
	if ! /sbin/modprobe -q dc_drmfbdev $PVR_DISPLAY_PARAMS; then
		echo "Module dc_drmfbdev failed to load. Retrying."
		if [ -z $depmod_has_been_run ]; then
			if [ -e /sbin/depmod ]; then
				echo "Running /sbin/depmod"
				/sbin/depmod && depmod_has_been_run=1
			fi
		fi
		if ! /sbin/modprobe -q dc_drmfbdev $PVR_DISPLAY_PARAMS; then echo "Continuing to load PowerVR services (dc_drmfbdev failed to load)"; dc_drmfbdev_failed=1; fi
	fi

			# Load the PVR Services module.
		#
		if ! /sbin/modprobe -q pvrsrvkm $PVR_SRVKM_PARAMS; then
		echo "Module pvrsrvkm failed to load. Retrying."
		if [ -z $depmod_has_been_run ]; then
			if [ -e /sbin/depmod ]; then
				echo "Running /sbin/depmod"
				/sbin/depmod && depmod_has_been_run=1
			fi
		fi
		if ! /sbin/modprobe -q pvrsrvkm $PVR_SRVKM_PARAMS ; then return; fi
	fi

		# Load the X Server.
		#
	/usr/bin/X -verbose -config /usr/etc/X11/xorg.conf &

	
	echo "Loaded PowerVR consumer services."
	return 0;
}

unload_pvr()
{
	# Stop the X Server.
	#
	[ -f /tmp/.X0-lock ] && read XPID < /tmp/.X0-lock && [ -n "$XPID" ] && kill $XPID && while [ -e /proc/$XPID ] ; do sleep 1; done

	# Unload the PVR Services module.
	#
	if /sbin/modprobe -r pvrsrvkm; then :; else return 1; fi

		# Unbind the display driver from fbcon so that it can be unloaded.
	#
		[ -f /sys/class/vtconsole/vtcon1/bind ] && echo 0 > /sys/class/vtconsole/vtcon1/bind; sleep 2
	
	# Unload 3rd party module(s).
	#
	if /sbin/modprobe -r dc_drmfbdev; then :; else echo "Warning: Could not unload dc_drmfbdev"; fi

	echo "Unloaded PowerVR consumer services."
	return 0;
}

# Deal with the type of invocation we get.
#
case "$1" in
"start")
	load_pvr
	;;
stop)
	if ! unload_pvr; then
		echo "Couldn't unload modules" >&2;
		exit 1
	fi
	;;
reload|restart)
	if unload_pvr; then
		load_pvr
	else
		echo "Couldn't unload modules" >&2;
		exit 1
	fi
	;;
*)
	echo "$0: unknown argument $1." >&2;
	;;
esac

